package com.css.fxfzysx.modules.vaolEngineering.controller;

import cn.hutool.core.io.FileUtil;
import com.css.fxfzysx.base.response.RestResponse;
import com.css.fxfzysx.modules.vaolEngineering.param.update.UpNetwork1Param;
import com.css.fxfzysx.modules.vaolEngineering.param.update.UpNetwork2Param;
import com.css.fxfzysx.modules.vaolEngineering.param.update.UpNetwork3Param;
import com.css.fxfzysx.modules.vaolEngineering.service.UpNetworkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 地下管网易损性分析与管理
 */
@RestController
@RequestMapping("upNetwork")
@Slf4j
public class UpNetworkController {

    @Autowired
    private UpNetworkService upNetworkService;

    /**
     * 数据验证
     */
    @GetMapping("getDataValidation")
    public RestResponse getDataValidation(String versionId,Integer type){
        try {
            HashMap<Integer, ArrayList<String>> map = upNetworkService.getDataValidation(versionId,type);
            return RestResponse.succeed(map);
        } catch (Exception e) {
            log.error("get getDataValidation error:{}",e.getMessage(),e);
            return RestResponse.fail("失败!");
        }
    }

    /**
     * 简单对应的模型对比计算和保存
     * @return
     */
    @GetMapping("countPseVulnerability")
    public RestResponse countPseVulnerability(String versionId,String tableId){
        try {
            upNetworkService.countPseVulnerability(versionId,tableId);
            return RestResponse.succeed("计算成功!");
        } catch (Exception e) {
            log.error("get countPseVulnerability error:{}",e.getMessage(),e);
            return RestResponse.fail("数据异常,计算失败!");
        }
    }

    /**
     * 连续焊接管道分析计算和保存
     * @return
     */
    @GetMapping("countContinuousWelding")
    public RestResponse countContinuousWelding(String versionId,String tableId){
        return upNetworkService.countContinuousWelding(versionId,tableId);
    }


    /**
     * 接口式管道分析方法计算和保存
     * @return
     */
    @GetMapping("countInterfaceWelding")
    public RestResponse countInterfaceWelding(String versionId,String tableId){
        return upNetworkService.countInterfaceWelding(versionId,tableId);
    }

    /**
     * 简单对应模型导入
     */
    @PostMapping("importPipelineDamageRate")
    public RestResponse importPipelineDamageRate(@RequestParam MultipartFile file){
        if (!FileUtil.getSuffix(file.getOriginalFilename()).equals("xls") && !FileUtil.getSuffix(file.getOriginalFilename()).equals("xlsx")) {
            return RestResponse.fail("文件格式不支持!");
        }
        return upNetworkService.importPipelineDamageRate(file);
    }

    /**
     * 地下管网-连续焊接管道分析方法导入
     */
    @PostMapping("importContinuousWelding")
    public RestResponse importContinuousWelding(@RequestParam MultipartFile file){
        if (!FileUtil.getSuffix(file.getOriginalFilename()).equals("xls") && !FileUtil.getSuffix(file.getOriginalFilename()).equals("xlsx")) {
            return RestResponse.fail("文件格式不支持!");
        }
        return upNetworkService.importContinuousWelding(file);
    }

    /**
     * 地下管网-接口式管道
     */
    @PostMapping("importInterfaceWelding")
    public RestResponse importInterfaceWelding(@RequestParam MultipartFile file){
        if (!FileUtil.getSuffix(file.getOriginalFilename()).equals("xls") && !FileUtil.getSuffix(file.getOriginalFilename()).equals("xlsx")) {
            return RestResponse.fail("文件格式不支持!");
        }
        return upNetworkService.importInterfaceWelding(file);
    }

    /**
     * 简单对应的模型对比计算和保存
     * @return
     */
    @PostMapping("countSimpleModel")
    public RestResponse countSimpleModel(@RequestBody UpNetwork1Param param){
        try {
            UpNetwork1Param count = upNetworkService.countSimpleModel(param);
            return RestResponse.succeed(count);
        } catch (Exception e) {
            log.error("post countSimpleModel error:{}",e.getMessage(),e);
            return RestResponse.fail("数据异常,计算失败!");
        }
    }

    /**
     * 连续焊接计算
     * @return
     */
    @PostMapping("continuousCount")
    public RestResponse continuousCount(@RequestBody UpNetwork2Param param){
        try {
            return upNetworkService.continuousCount(param);
        } catch (Exception e) {
            log.error("post continuousCount error:{}",e.getMessage(),e);
            return RestResponse.fail("数据异常,计算失败!");
        }
    }

    /**
     * 接口式计算
     * @return
     */
    @PostMapping("interfaceCount")
    public RestResponse interfaceCount(@RequestBody UpNetwork3Param param){
        try {
            return upNetworkService.interfaceCount(param);
        } catch (Exception e) {
            log.error("post continuousCount error:{}",e.getMessage(),e);
            return RestResponse.fail("数据异常,计算失败!");
        }
    }

}
